using System;
using System.Collections.Concurrent;

namespace TelerikMvcGridCustomBindingHelper.Mapper
{
    class PropertyMap
    {
        public readonly Type EntityType;
        public readonly Type ViewModelType;
        public readonly string Key;
        public readonly ConcurrentDictionary<string, GridPropertyInfo> Properties = new ConcurrentDictionary<string, GridPropertyInfo>();

        public PropertyMap(Type entityType, Type viewModelType, GridPropertyInfo entityMemberInfo, string viewModelPropertyName)
        {
            EntityType = entityType;
            ViewModelType = viewModelType;
            Key = BuildKey(EntityType, ViewModelType);
            AddProperty(entityMemberInfo, viewModelPropertyName);
        }

        private PropertyMap(Type entityType, Type viewModelType)
        {
            EntityType = entityType;
            ViewModelType = viewModelType;
            Key = string.Concat(EntityType.FullName, ViewModelType.FullName);
        }

        public void AddProperty(GridPropertyInfo entityMemberInfo, string viewModelPropertyName)
        {
            Properties.TryAdd(viewModelPropertyName, entityMemberInfo);
        }

        public static string BuildKey(Type entityType, Type viewModelType)
        {
            return string.Concat(entityType.FullName, viewModelType.FullName);
        }

        public static string BuildKey<TEntity, TViewModel>()
        {
            return BuildKey(typeof(TEntity), typeof(TViewModel));
        }

        public static PropertyMap Stub<TEntity, TViewModel>()
        {
            return new PropertyMap(typeof(TEntity), typeof(TViewModel));
        }

        public static PropertyMap Stub(Type entityType, Type viewModelType)
        {
            return new PropertyMap(entityType, viewModelType);
        }
    }
}